package com.dlz.systemshop.controller;

import com.dlz.systemshop.pojo.*;
import com.dlz.systemshop.service.GoodsService;
import com.dlz.systemshop.utils.ServerAddressUtil;
import com.dlz.systemshop.utils.UpLoadUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RequestMapping("/goods")
@RestController
@Slf4j
public class GoodsController {
    @Autowired
    private GoodsService goodService;

    // 不需要重新设置路径的路径 （如/static）
    private static final String[] NO_RESET_PATH = {"static"};

    // 单个图片路径
    private static final String GOODS_SINGLE = "upload/image/goods/single";

    // 返回 goodsId
    @PostMapping("/add")
    @Transactional
    public Integer add(@RequestBody Goods goods) {
        log.info("添加商品 goods: {}", goods);
        Assert.notNull(goods, "商品不能为空");
        Assert.notNull(goods.getCategoryId(), "商品分类不能为空");
        Integer goodsId = goodService.add(goods);
        return goodsId;
    }

    @PostMapping("/update")
    public Boolean update(@RequestBody Goods goods) {
        log.info("更新商品 goods: {}", goods);
        Assert.notNull(goods, "商品不能为空");
        Boolean flag = goodService.update(goods);
        return flag;
    }

    @GetMapping("/getGoods")
    public Goods getGoods(Integer id, HttpServletRequest request) {
        log.info("获取商品 id: {}", id);
        Assert.notNull(id, "商品Id不能为空");
        Goods goods = goodService.getGoods(id);
        String picture = goods.getPicture();
        // 判断是否需要重新设置
        if (!picture.isEmpty() && isResetPath(picture)) {
            String path = ServerAddressUtil.getURLExURI(request);
            goods.setPicture(path + picture);
        }
        return goods;
    }

    private static Boolean isResetPath(String path) {
        for (String s : NO_RESET_PATH) {
            if (path.startsWith(s)) {
                return false;
            }
        }
        return true;
    }


    @GetMapping("/getGoodsListAll")
    public List<Goods> getGoodsListAll() {
        List<Goods> goodsListAll = goodService.getGoodsListAll();
        return goodsListAll;
    }

    @GetMapping("/getPageDivision")
    public PageResult<Goods> getPageDivision(PageRequest pageRequest) {
        List<Goods> goodsByPage = goodService.findGoodsByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        Integer count = goodService.getCount();
        return new PageResult<Goods>(pageRequest.getCurrentPage(), pageRequest.getPageSize(), count, goodsByPage);
    }

    @RequestMapping("/delGoods")
    public Boolean delGoods(@RequestBody Map<String, String> params) {
        Integer id = Integer.parseInt(params.get("id"));
        log.info("删除商品 id: {}", id);
        return goodService.delGoods(id);
    }

    @RequestMapping("/submitPic")
    public String submitPic(@RequestParam("file") MultipartFile file,@RequestParam("dest") String dest) {
        log.info("上传商品图片 file: {} 图片存储地址 dest: {}", file, dest);
        String filePath = UpLoadUtil.uploadImage(file, dest);
        return filePath;
    }

}
